<?php


namespace App\Traits;


use Elasticsearch\ClientBuilder;

trait Elasticsearch
{
    private static $elasticsearch;

    /**
     * @param mixed ...$args
     * @return static
     */
    static function getElasticsearchInstance(...$args)
    {
        if(!isset(static::$elasticsearch)){
            file_put_contents(storage_path() . '/logs/elasticTime' . date('Ymd') . '.log',
                '开始连接' . microtime(true). PHP_EOL, FILE_APPEND
            );
            $builder = ClientBuilder::create();


            $builder->setHosts(config('elasticsearch.connections'));
            //$builder->setBasicAuthentication('elastic', 'password');
            $builder->setRetries(config('elasticsearch.retries'));
            static::$elasticsearch = $builder->build();
            file_put_contents(storage_path() . '/logs/elasticTime' . date('Ymd') . '.log',
                '结束连接' . microtime(true). PHP_EOL, FILE_APPEND
            );
        }
        return static::$elasticsearch;
    }
}